package com.hontech.bread.device.mdb

import com.hontech.bread.utils.pow10
import serial.codec.FlexArray
import java.math.BigDecimal
import java.math.RoundingMode


class CoinSetupInfo(
    val level: Int,
    val currencyCode: Int,
    val scaling: Int, // ${stringById(R.string.t1733460848_190)}
    val decimalPlaces: Int, // ${stringById(R.string.t1733460848_191)}
    val coinTypeRouting: Int,
    private val coinTypeCredit: FlexArray
) {

    companion object {

        private var instance: CoinSetupInfo? = null

        fun get(): CoinSetupInfo {
            return instance ?: throw IllegalStateException("coin not init")
        }
    }

    init {
        instance = this
    }

    fun priceValue(price: String): Int {
        val n = BigDecimal(price).multiply(BigDecimal(pow10(decimalPlaces))).intValueExact()
        if ((n % scaling) != 0) {
            throw IllegalStateException("price invalid:$price")
        }
        return n / scaling
    }

    fun valuePrice(value: Int): String {
        val bd = BigDecimal(value)
        val n = bd.div(BigDecimal(pow10(decimalPlaces)))
        n.setScale(decimalPlaces, RoundingMode.DOWN)
        return n.toPlainString()
    }

    fun valueByType(type: Int): Int {
        return coinTypeCredit.getData(type and 0x0F) * scaling
    }
}


class BillSetupInfo(
    val level: Int,
    val countryCode: Int,
    val scaling: Int, // ${stringById(R.string.t1733460848_192)}
    val decimalPlaces: Int, // ${stringById(R.string.t1733460848_193)}
    val capacity: Int, // ${stringById(R.string.t1733460848_194)}
    val security: Int, // ${stringById(R.string.t1733460848_195)}
    val escrow: Int, // ${stringById(R.string.t1733460848_196)}
    val billTypeCredit: FlexArray
) {

    companion object {
        private var instance: BillSetupInfo? = null

        fun get(): BillSetupInfo {
            return instance ?: throw IllegalStateException("bill not init")
        }
    }

    init {
        instance = this
    }

    data class Info(
        val value: Int
    )

    fun allowTypes(): Int {
        var s = 0
        for (i in 0 until 16) {
            val n = billTypeCredit.getData(i)
            if ((n != 0) && (n != 0xFF)) {
                s += 0x01 shl i
            }
        }
        return s
    }

    fun priceValue(price: String): Int {
        val n = BigDecimal(price).multiply(BigDecimal(pow10(decimalPlaces))).intValueExact()
        if ((n % scaling) != 0) {
            throw IllegalStateException("price invalid:$price")
        }
        return n / scaling
    }

    fun valuePrice(value: Int): String {
        val bd = BigDecimal(value)
        val n = bd.div(BigDecimal(pow10(decimalPlaces)))
        n.setScale(decimalPlaces, RoundingMode.DOWN)
        return n.toPlainString()
    }

    fun valueByType(type: Int): Int {
        return billTypeCredit.getData(type and 0x0F) * scaling
    }

    fun infos(): ArrayList<Info> {
        val arr = ArrayList<Info>()
        for (i in 0 until 16) {
            val n = billTypeCredit.getData(i)
            if ((n != 0) && (n != 0xFF)) {
                arr.add(Info(n * scaling))
            }
        }
        return arr
    }

    fun isEscrow() = escrow == 0xFF
}


class PosSetupInfo(
    val level: Int,
    val currencyCode: Int,
    val scaling: Int,
    val decimalPlaces: Int,
    val connTimeout: Int, // sec
    val features: Int
) {

    companion object {
        private var instance: PosSetupInfo? = null

        fun get(): PosSetupInfo {
            return instance ?: throw IllegalStateException("pos not init")
        }
    }

    init {
        instance = this
    }

    fun priceValue(price: String): Int {
        val n = BigDecimal(price).multiply(BigDecimal(pow10(decimalPlaces))).intValueExact()
        if ((n % scaling) != 0) {
            throw IllegalStateException("price invalid:$price")
        }
        return n / scaling
    }
}













